package zpe.genericcrud.jsf.view.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

public class EnumConverter implements Converter {
	
	@SuppressWarnings({ "deprecation", "unchecked", "rawtypes" })
	public Object getAsObject(FacesContext context, UIComponent comp, String value) throws ConverterException {
		if(value == null) return value;
		try {
			Class enumType = comp.getValueBinding("value").getType(context);
			return Enum.valueOf(enumType, value);
		} catch (IllegalArgumentException e) {
			return null;
	    }
	}


	@Override
	public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
		return (String)object;
	}

}
